//
//  MainViewController.h
//  RunSketch
//
//  Created by Brian on 10/1/09.
//  Copyright Tellart 2009. All rights reserved.
//

#import "UserPreferencesController.h"
#import "NADAMobileIOHelper.h"
#import "FlipsideViewController.h"
//#import "SketchWebView.h"
#import "TCPSocketIOPackTransport.h"
#import "LocalSerialIOPackTransport.h"
#import "HTTPServer.h"

#define kJsTRUE @"true"
#define kJsFALSE @"false" 

@interface MainViewController : UIViewController
    <FlipsideViewControllerDelegate,
    UIWebViewDelegate,
    NADAMobileIOHelperDelegate,
    UIImagePickerControllerDelegate,
    UINavigationControllerDelegate
    > {
    
    //FlipsideViewController      *dangerousFlipside;
    
    UserPreferencesController   *userPrefs;

    int                         sketchLoadPhase2Countdown;
    BOOL                        sketchLoaded;
    BOOL                        sketchIsReloading;
    BOOL                        updateLoopCreated;
    BOOL                        updateLoopEnabled;
    
    UIActivityIndicatorView     *sketchLoadingActivityIndicator;
    UIWebView                   *sketchWebView;
    UIView                      *mpVolumeViewParentView;
    NADAMobileIOHelper          *ioHelper;
    UIImagePickerController     *cameraController;
    UIImage                     *lastCameraImage;
    BOOL                        saveLastCameraImageAsFullRes;
    NSURL                       *lastCameraImageURL;
    
    TCPSocketIOPackTransport    *remoteIOPackTransport;
    LocalSerialIOPackTransport  *localIOPackTransport;

    //HTTP server to allow auto-updating
	HTTPServer                  *httpServer;
	NSDictionary                *addresses;
    BOOL autoUpdateRequested;
    
	//to use timer looping, uncomment the next line
    NSTimer                     *updateTimer;
    int                         updateLoopCount;
    
    BOOL remoteIOPackEnabled;
    BOOL localIOPackEnabled;
    BOOL sensorToneState;
    BOOL sensorProximityState;
    
    BOOL warnedAboutMissingCamera;
}

//@property (nonatomic, retain) FlipsideViewController *dangerousFlipside;
@property (nonatomic, retain) IBOutlet UIWebView *sketchWebView;
@property (nonatomic, retain) IBOutlet UIView *mpVolumeViewParentView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *sketchLoadingActivityIndicator;
@property (nonatomic, retain) UIImagePickerController *cameraController;
@property (nonatomic, retain) UIImage *lastCameraImage;
@property (nonatomic, retain) NSURL *lastCameraImageURL;

- (IBAction)showInfo;
- (IBAction) loadSketch;
- (IBAction) reloadSketch;

- (void)setupSketchAfterLoad;
- (void) setRemoteIOPackHostName:(NSString *)hostName;

- (NSString *) flipsideViewControllerGetRemoteIOPackHostName;
- (BOOL) flipsideViewControllerWasAutoUpdateRequested;
- (BOOL) flipsideViewControllerGetEnableLocalIOPack;
- (BOOL) flipsideViewControllerIsRemoteIOPackConnected;
- (BOOL) flipsideViewControllerIsRemoteIOPackError;
- (BOOL) flipsideViewControllerIsLocalIOPackConnected;
- (BOOL) flipsideViewControllerIsLocalIOPackError;
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller reloadSketch:(BOOL)reload;
- (void)flipsideViewControllerDidChangeRemoteIOPackHostName:(FlipsideViewController *)controller to:(NSString*)hostName;
- (void)flipsideViewControllerDidChangeFlowControl:(FlipsideViewController *)controller to:(BOOL)state;
- (void)flipsideViewControllerDidChangeIOPackBoost:(FlipsideViewController *)controller to:(BOOL)state;

- (void) initAutoUpdateServer;
- (void) deallocAutoUpdateServer;
- (void) localhostAddressesResolved:(NSNotification *) notification;
- (void) startAutoUpdateServer;
- (void) stopAutoUpdateServer;

- (void) enableSensors;
- (void) disableSensors;

- (void) disableIOPacks;
- (void) enableIOPacks;

- (void) enableIOHelper;
- (void) disableIOHelper;

- (void) initializeUpdateLoop;
- (void) startUpdateLoop;
- (void) stopUpdateLoop;
- (void) initializeUpdateLoopOnTimer;

- (void) updateLoopRun;
- (void) updateIOPackInputs:(IOPack *)ioPack;
- (void) updateIOPackOutputs:(IOPack *)ioPack withNewValues:(NSArray *)parts;
- (void) pollSketchForReverseCalls;

@end
